//
//  MineViewController.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/6/29.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "MineViewController.h"
#import "Mine_NavBar.h"
#import "MineCell.h"
#import "SettingVC.h"
#import "StaffManagerVC.h"
#import "RegistDelegateVC.h"
#import "CompanyInfoVC.h"

@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource,TZImagePickerControllerDelegate> {
    NSString *service_phone;
}

@property (nonatomic, strong) UIImageView *bgView;
@property (nonatomic, strong) Mine_NavBar *navBar;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation MineViewController

-(Mine_NavBar *)navBar {
    if (!_navBar) {
        _navBar = [[Mine_NavBar alloc] initWithFrame:CGRectMake(0, 0, ScreenW, StatusBarHeight + 124)];
        __weak_self;
        _navBar.iconBlock = ^{
            [weakself chooseIcon];
        };
    }
    return _navBar;
}

-(UIImageView *)bgView {
    if (!_bgView) {
        _bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 345)];
        _bgView.image = [UIImage imageNamed:@"tab_my_bg"];
    }
    return _bgView;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.navBar.nim_bottom, ScreenW, ScreenH - TabbarHeight - self.navBar.nim_bottom) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.bounces = NO;
    }
    return _tableView;
}

- (void)chooseIcon {
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 columnNumber:1 delegate:self pushPhotoPickerVc:YES];
    imagePickerVc.isSelectOriginalPhoto = YES;
    imagePickerVc.allowTakeVideo = NO;   // 在内部显示拍视频按
    imagePickerVc.iconThemeColor = [UIColor colorWithRed:31 / 255.0 green:185 / 255.0 blue:34 / 255.0 alpha:1.0];
    imagePickerVc.showPhotoCannotSelectLayer = YES;
    imagePickerVc.cannotSelectLayerColor = [[UIColor whiteColor] colorWithAlphaComponent:0.8];
    [imagePickerVc setPhotoPickerPageUIConfigBlock:^(UICollectionView *collectionView, UIView *bottomToolBar, UIButton *previewButton, UIButton *originalPhotoButton, UILabel *originalPhotoLabel, UIButton *doneButton, UIImageView *numberImageView, UILabel *numberLabel, UIView *divideLine) {
        [doneButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    }];
    // 你可以通过block或者代理，来得到用户选择的照片.
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        [[TZImageManager manager] getOriginalPhotoDataWithAsset:assets.firstObject progressHandler:^(double progress, NSError *error, BOOL *stop, NSDictionary *info) {
            
        } completion:^(NSData *data, NSDictionary *info, BOOL isDegraded) {
            [self requestForUpload:data];
        }];
        //通过asset获取原图，方法会先返回缩略图，再返回原图
//        [[TZImageManager manager] getOriginalPhotoWithAsset:assets.firstObject newCompletion:^(UIImage *photo, NSDictionary *info, BOOL isDegraded) {
//            if ([info[PHImageResultIsDegradedKey] boolValue]) {
//                //返回的是缩略图
//            }else {
//                //返回的是原图
//                [self requestForUpload:photo];
//            }
//        }];
    }];
    [self presentViewController:imagePickerVc animated:YES completion:nil];
}

- (void)requestForUpload:(NSData *)image {
    [MBProgressHUD leShowHUDWithMessage:@"上传中" activity:YES];
//    [HttpRequest postImgWithUrl:ReceiptDeliveryModifyHeadIcon imglist:@[image] imgFile:@"file" withParameter:@{} sucess:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
//        if (code) {
//            [self requestForInfo];
//        }
//    } failure:^(NSError * _Nonnull error) {
//
//    }];
    [HttpRequest postImgDataWithUrl:ReceiptDeliveryModifyHeadIcon imgData:image imgFile:@"file" withParameter:@{} sucess:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [self requestForInfo];
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    [self.view addSubview:self.bgView];
    [self.view addSubview:self.navBar];
    [self.view addSubview:self.tableView];
    service_phone = @"";
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self requestForInfo];
}

- (void)requestForInfo {
    [HttpRequest PostWithApi:ReceiptDeliveryPersonInfo params:@{} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self.navBar.title = [NSString stringWithFormat:@"%@(%@)",[responseObject objectForKey:@"s_name"],[responseObject objectForKey:@"company_no"]];
            self.navBar.icon = [responseObject objectForKey:@"headimg"];
            NSMutableString *str = [NSMutableString string];
            if (![FactoryMethod isNull:[responseObject objectForKey:@"department"]]) {
                [str appendFormat:@"%@",[responseObject objectForKey:@"department"]];
            }
            if (![FactoryMethod isNull:[responseObject objectForKey:@"name"]]) {
                if (str.length) {
                    [str appendFormat:@"-%@",[responseObject objectForKey:@"name"]];
                }else {
                    [str appendFormat:@"%@",[responseObject objectForKey:@"name"]];
                }
            }
            self.navBar.name = str;
            self->service_phone = [responseObject objectForKey:@"service_phone"];
            [self.tableView reloadData];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 3;
    }
    return 2;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MineCell *cell = [MineCell MineCellWithTableView:tableView];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.iconUrl = @"icon_staff";
            cell.titleText = @"员工管理";
            cell.showLine = YES;
        }else if (indexPath.row == 1) {
            cell.iconUrl = @"icon_enterprise";
            cell.titleText = @"企业信息";
            cell.showLine = YES;
        }else if (indexPath.row == 2) {
            cell.iconUrl = @"icon_set";
            cell.titleText = @"设置";
            cell.showLine = NO;
        }
    }else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.iconUrl = @"icon_contact";
            cell.titleText = @"联系客服";
            cell.showLine = YES;
        }else if (indexPath.row == 1) {
            cell.iconUrl = @"icon_e9";
            cell.titleText = @"关于 IDA 云物流";
            cell.showLine = NO;
        }
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            StaffManagerVC *vc = [StaffManagerVC new];
            vc.hidesBottomBarWhenPushed = YES;
            [self.rt_navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 1) {
            CompanyInfoVC *vc = [CompanyInfoVC new];
            vc.hidesBottomBarWhenPushed = YES;
            [self.rt_navigationController pushViewController:vc animated:YES];
        }else if (indexPath.row == 2) {
            SettingVC *vc = [SettingVC new];
            vc.hidesBottomBarWhenPushed = YES;
            [self.rt_navigationController pushViewController:vc animated:YES];
        }
    }else if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            if (service_phone.length) {
                NSString *str = [[NSMutableString alloc] initWithFormat:@"tel:%@",service_phone];
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str] options:@{} completionHandler:nil];
            }
        }else if (indexPath.row == 1) {
            RegistDelegateVC *vc = [RegistDelegateVC new];
            vc.hidesBottomBarWhenPushed = YES;
            [self.rt_navigationController pushViewController:vc animated:YES];
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    MineCell *cell = [MineCell MineCellWithTableView:tableView];
    return cell.height;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
